package com.ocean.admin.controller;


import com.ocean.admin.core.annotation.Log;
import com.ocean.admin.core.annotation.enums.BusinessType;
import com.ocean.admin.model.dto.dept.DeptQueryDTO;
import com.ocean.admin.model.dto.dept.DeptSaveDTO;
import com.ocean.admin.model.dto.dept.DeptUpdateDTO;
import com.ocean.admin.model.vo.dept.DeptVo;
import com.ocean.admin.service.ISysDeptService;
import com.ocean.core.util.response.ResultSet;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Tag(name = "部门管理")
@RestController
@RequestMapping("/admin/sys-dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService iSysDeptService;

    /**
     * 获取部门列表树
     *
     * @param queryDTO 查询条件
     * @return 部门树
     */
    @Operation(summary = "查询部门树")
    @PreAuthorize("@ss.hasPermission('system:dept:listTree')")
    @PostMapping(value = "/listTree")
    public ResultSet<List<DeptVo>> listTree(@RequestBody(required = false) DeptQueryDTO queryDTO) {
        return ResultSet.ok(iSysDeptService.listTree(queryDTO));
    }

    /**
     * 新增部门
     *
     * @param saveDTO 部门信息
     * @return 结果
     */
    @Operation(summary = "新增部门")
    @PreAuthorize("@ss.hasPermission('system:dept:saveDept')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "saveDept")
    public ResultSet<String> saveDept(@Validated @RequestBody DeptSaveDTO saveDTO) {
        return ResultSet.ok(iSysDeptService.deptSave(saveDTO));
    }

    /**
     * 修改部门
     *
     * @param updateDTO 部门信息
     * @return 结果
     */
    @Operation(summary = "修改部门")
    @PreAuthorize("@ss.hasPermission('system:dept:updateDept')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateDept")
    public ResultSet<String> updateDept(@Validated @RequestBody DeptUpdateDTO updateDTO) {
        return ResultSet.ok(iSysDeptService.updateDept(updateDTO));
    }

    /**
     * 删除部门
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Operation(summary = "删除部门")
    @PreAuthorize("@ss.hasPermission('system:dept:removeDept')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @GetMapping("removeDept/{deptId}")
    public ResultSet<String> removeDept(@PathVariable Long deptId) {
        return ResultSet.ok(iSysDeptService.removeDept(deptId));
    }
}
